package com.zzj.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    @Pointcut("execution(* com.zzj.service.*.*(..))")
    public void pointcut(){}

    @Before("pointcut()")
    private void before(){
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        System.out.println(methodName + "方法被调用了====before...");
    }
    @After("pointcut()")
    private void after(){
        String methodName = new Exception().getStackTrace()[0].getMethodName();
        System.out.println(methodName + "方法被调用了====after...");
    }
    @AfterReturning("pointcut()")
    private void afterReturn(){
        String methodName = new Exception().getStackTrace()[0].getMethodName();
        System.out.println(methodName + "方法被调用了====afterRunning...");
    }
    @AfterThrowing("pointcut()")
    private void afterThrowing(){
        String methodName = new Exception().getStackTrace()[0].getMethodName();
        System.out.println(methodName + "方法被调用了====afterThrowing...");
    }

    //@Around("pointcut()")
    public void around(ProceedingJoinPoint joinPoint){
        try {
            before();
            joinPoint.proceed();
            afterReturn();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            afterThrowing();
        } finally {
            after();
        }
    }
}
